package main

import (
	"fmt"
	"time"
)

var hasError bool

func main() {
	fmt.Println("in main")
	
	hasError = false
	defer fmt.Println("defer")
	go func() {
		defer recovery()
		for {
			fmt.Println("waiting error...", hasError)
			if hasError {
				panic("error occur...")
			}
			time.Sleep(1 * time.Second)
		}
	}()
	

	time.Sleep(3 * time.Second)
	hasError = true
	fmt.Println("set error ...", hasError)

	time.Sleep(10 * time.Second)
	fmt.Println("main exit ...")

}

func recovery() {
	if err := recover(); err != nil {
		fmt.Println("recover get err",err) // 这里的err其实就是panic传入的内容
	}
}